<template>
  <div class="app-container">
    <el-form ref="form"   :rules="rules" :model="form" label-width="150px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="选择公司" prop="branchOfficeId">
            <el-select v-model="form.branchOfficeId" placeholder="请选择" clearable>
              <el-option
                v-for="item in paymentCate"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="选择缴款类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择" clearable filterable>
              <el-option
                v-for="item in ProjectClass"
                :key="item.value"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="金额" prop="amount">
            <el-input v-model.number="form.amount"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="缴款时间" prop="time">
            <el-date-picker
              v-model="form.time"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetime"
              placeholder="选择日期时间"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input size="medium" type="textarea" :rows="4" v-model="form.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item>
            <el-button type="primary" @click="addPayment('form')">立即提交</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { addPayment, getBranch } from "@/api/paymentDetails";
export default {
  name: "paymentAdd",
  data() {
    return {
      form: {
        branchOfficeId: null,
        type: null,
        amount: null,
        time: "",
        remark: "",
        types:[1,2],
      },
      
      paymentCate: [
      ],
      // 缴款类型 1:首次缴款 2:补缴款 3:抵扣款 4:其它
      ProjectClass: [
        { id: 1, name: "首次缴款" },
        { id: 2, name: "补缴款" },
        { id: 3, name: "抵扣款" },
        { id: 4, name: "其它" }
      ],
      rules: {
        branchOfficeId: [
          { required: true, message: "请选择", trigger: "change" }
        ],
        type: [{ required: true, message: "请选择", trigger: "change" }],
        amount: [{type: 'number',required: true, message: "请输入金额", trigger: "blur" }],
        time: [{required: true, message: "请输入时间", trigger: "change" }],
        remark: [{ required: true, message: "请输入备注", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getBranch();
  },
  mounted() {
    // console.log(this.$route.query);
    // this.form.id = Number(this.$route.query.id);
  },
  watch: {},
  methods: {
    //获取分公司
    getBranch() {
      getBranch(this.form).then(res => {
        console.log(res);
        this.paymentCate=res.data.data
      });
    },
    //立即提交
    addPayment(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let datas = { ...this.form };
          // console.log(datas)
          delete datas.types
          addPayment(datas).then(res => {
            console.log(res);
            if (res.data.code == 0) {
              this.$message({
                showClose: true,
                message: "添加成功",
                type: "success"
              });
              this.$router.push("/paymentDetails");
            } else {
              this.$message({
                showClose: true,
                message: res.data.msg,
                type: "warning"
              });
            }
          });
        } else {
          this.$message({
            showClose: true,
            message: "有必填项没填",
            type: "warning"
          });
          return false;
        }
      });
    }
  }
};
</script>
<style scoped>
.app-container {
  padding: 20px !important;
}
</style>